package com.longcai.service.fuyoupay.impl;

import java.util.Date;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.payment.fuyou.request.ClyjFuyouBankcardChange;
import com.longcai.entity.payment.fuyou.request.ClyjFuyouCompanyRegister;
import com.longcai.entity.payment.fuyou.request.ClyjFuyouSmallcompanyRegister;
import com.longcai.fenzhuang.fuyou.BaseResponse;
import com.longcai.fenzhuang.fuyou.FuyouCommonPortMethod;
import com.longcai.mapper.fuyoupay.ClyjFuyouBankcardChangeMapper;
import com.longcai.service.fuyoupay.ClyjFuyouBankcardChangeService;
import com.longcai.service.fuyoupay.ClyjFuyouCompanyRegisterService;
import com.longcai.service.fuyoupay.ClyjFuyouSmallcompanyRegisterService;



/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LWK
 * @since 2019-04-17
 */
@Service
public class ClyjFuyouBankcardChangeServiceImpl extends ServiceImpl<ClyjFuyouBankcardChangeMapper, ClyjFuyouBankcardChange> implements ClyjFuyouBankcardChangeService {
	@Autowired
	private ClyjFuyouCompanyRegisterService clyjFuyouCompanyRegisterService;
	@Autowired
	private ClyjFuyouSmallcompanyRegisterService clyjFuyouSmallcompanyRegisterService;
	private  Logger logger=LoggerFactory.getLogger(getClass());
	@Override
	public ApiResult<ClyjFuyouBankcardChange> checkout(JSONObject params) {
		ClyjFuyouBankcardChange cfbc = new ClyjFuyouBankcardChange();
		cfbc.setId(UUID.randomUUID().toString().replace("-", ""));
		cfbc.setLoginId(params.getString("loginId"));
		cfbc.setUserType(params.getString("userType"));
		cfbc.setMobileNo(params.getString("mobileNo"));
		cfbc.setCustNm(params.getString("custNm"));
		cfbc.setBankLicense(params.getString("bankLicense"));
		cfbc.setArtifNm(params.getString("artifNm"));
		cfbc.setCityId(params.getString("cityId"));
		cfbc.setBanknNm(params.getString("bankNm"));
		cfbc.setParentBankid(params.getString("parentBankid"));
		cfbc.setCapacntno(params.getString("capacntno"));
		cfbc.setCreateTime(new Date());
		FuyouCommonPortMethod fcpm = new FuyouCommonPortMethod();
		BaseResponse r = fcpm.bankCardChange(cfbc);
		if (r.getRespCode().equals("0000")||r.getRespCode().equals("0127")) {
			if (cfbc.getUserType().equals("0")) {                         //用户类型UserType=0代表企业开户
				ClyjFuyouCompanyRegister cfcr = new ClyjFuyouCompanyRegister();
				cfcr.setMobileNo(cfbc.getMobileNo());
				cfcr.setCustName(cfbc.getCustNm());
				cfcr.setBankLicense(cfbc.getBankLicense());
				cfcr.setArtifNm(cfbc.getArtifNm());
				cfcr.setCityId(cfbc.getCityId());
				cfcr.setParentBankId(cfbc.getParentBankid());
				cfcr.setBankNm(cfbc.getBanknNm());
				cfcr.setCapacntNo(cfbc.getCapacntno());
				cfcr.setId(params.getString("id"));
				cfcr.setUpdateTime(cfbc.getCreateTime());
				boolean a = clyjFuyouCompanyRegisterService.updateById(cfcr);
				if (!a) {
					logger.error("入库失败");
					return ApiResult.fail("跟换银行卡失败");
				}
			}else if(cfbc.getUserType().equals("2")){                     //用户类型UserType=2代表个人/小微企业
				ClyjFuyouSmallcompanyRegister cfsr=new ClyjFuyouSmallcompanyRegister();
				cfsr.setMobileNo(cfbc.getMobileNo());
				cfsr.setArtifNm(cfbc.getArtifNm());
				cfsr.setCityId(cfbc.getCityId());
				cfsr.setParentBankid(cfbc.getParentBankid());
				cfsr.setBankNm(cfbc.getBanknNm());
				cfsr.setCapacntNo(cfbc.getCapacntno());
				cfsr.setId(params.getString("id"));
				cfsr.setUpdateTime(cfbc.getCreateTime());
				boolean c = clyjFuyouSmallcompanyRegisterService.updateById(cfsr);
				if(!c){
					logger.error("入库失败");
					return ApiResult.fail("跟换银行卡失败");
				}
			}
			return ApiResult.success(cfbc);
		}
		return ApiResult.fail("更换银行卡失败");
	}

}
